// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TermFrequencyCounter.cs" company="Arizona State University">
//   Copyright (C) 2011-2012 Sukru Tikves and Arizona State University
//   
//   Released under Microsoft Public License (Ms-PL)
//   See http://cipsdata.codeplex.com/license for up to date license 
//   informaiton.
// </copyright>
// <summary>
//   The FrequencyCounter interface.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Cips.DataMining
{
    using System.Collections.Generic;

    /// <summary>
    /// The FrequencyCounter interface.
    /// </summary>
    /// <typeparam name="T">
    /// Keyword type.
    /// </typeparam>
    public interface IFrequencyCounter<T> where T : class
    {
        /// <summary>
        /// Gets the frequencies.
        /// </summary>
        TermVector<T> Frequencies { get; }

        /// <summary>
        /// Adds the given document.
        /// </summary>
        /// <param name="document">The document.</param>
        void AddDocument(IEnumerable<T> document);

        /// <summary>
        /// Adds the given document.
        /// </summary>
        /// <param name="document">The document.</param>
        void AddDocument(IEnumerable<int> document);

        /// <summary>
        /// Calculates the frequencies.
        /// </summary>
        void Calculate();
    }

    /// <summary>
    /// Implements the TermFrequency algorithm.
    /// </summary>
    /// <typeparam name="T">
    /// Keyword type.
    /// </typeparam>
    public class TermFrequencyCounter<T> : IFrequencyCounter<T> where T : class
    {
        /// <summary>
        /// The frequencies.
        /// </summary>
        readonly SparseTermVector<T> frequencies;

        /// <summary>
        /// Initializes a new instance of the <see cref="TermFrequencyCounter&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        public TermFrequencyCounter(TermDictionary<T> dictionary)
        {
            frequencies = new SparseTermVector<T>(dictionary);
        }

        /// <summary>
        /// Gets the frequencies.
        /// </summary>
        public TermVector<T> Frequencies
        {
            get { return frequencies; }
        }

        /// <summary>
        /// Adds the given document.
        /// </summary>
        /// <param name="document">The document.</param>
        public void AddDocument(IEnumerable<T> document)
        {
            foreach (var term in document)
                frequencies[term]++;
        }

        /// <summary>
        /// Adds the given document.
        /// </summary>
        /// <param name="document">The document.</param>
        public void AddDocument(IEnumerable<int> document)
        {
            foreach (var term in document)
                frequencies[term]++;
        }

        /// <summary>
        /// Calculates the frequencies. (Not necessary for TermFrequency).
        /// </summary>
        public void Calculate()
        {
        }
    }
}